var lib = {
    sort: function(array, key, left, right) {
        if (left < right) {
            var pivot = split(array, key, left, right);
            sort(array, key, left, split - 1);
            sort(array, key, split + 1, right);
        }
    },
    split: function(array, key, left, right) {
        var i = left;
        var j = right - 1;
        var pivot = array[right][key];
        do {
            while (array[i][key] <= pivot && i < right) {
                i++;
            }
            while (array[j][key] >= pivot && j > left) {
                j--;
            }
            if (i < j) {
                var tmp = array[i][key];
                array[i][key] = array[j][key];
                array[j][key] = tmp;
            }
        } while (i < j);
        if (array[i][key] > pivot) {
            var tmp = array[i][key];
            array[i][key] = array[right][key];
            array[right][key] = tmp;
        }
    },
    dateToString: function(date, locale) {
        var diff = new Date() / 1e3 - date;
        date = new Date(1e3 * date);
        if (diff < 5) {
            return languages[locale].few_seconds;
        } else if (diff < 60) {
            return languages[locale].ago1 + Math.ceil(diff) + " " + languages[locale].seconds + languages[locale].ago2;
        } else if (diff < 3600) {
            return languages[locale].ago1 + Math.ceil(diff / 60) + " " + languages[locale].minutes + languages[locale].ago2;
        } else if (diff < 86400) {
            return languages[locale].ago1 + Math.ceil(diff / 3600) + " " + languages[locale].hours + languages[locale].ago2;
        } else if (diff < 172800 && date.getDay() == new Date(new Date() - 864e5).getDay()) {
            return languages[locale].yesterday + " " + languages[locale].at + " " + lib.pad(date.getHours(), 2) + "." + lib.pad(date.getMinutes(), 2) + languages[locale].oclock;
        } else if (diff < 604800) {
            return languages[locale].days[date.getDay()] + " " + languages[locale].at + " " + lib.pad(date.getHours(), 2) + "." + lib.pad(date.getMinutes(), 2) + languages[locale].oclock;
        } else {
            return lib.pad(date.getDate(), 2) + "." + lib.pad(date.getMonth() + 1, 2) + "." + date.getFullYear() + " " + languages[locale].at + " " + lib.pad(date.getHours(), 2) + "." + lib.pad(date.getMinutes(), 2) + languages[locale].oclock;
        }
    },
    pad: function(number, length) {
        var str = "" + number;
        while (str.length < length) {
            str = "0" + str;
        }
        return str;
    }
};